package com.ucomm.talent.activity;


import me.maxwin.view.XListView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.adapter.CategoryInfoAdapter;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.widget.CustomProgressDialog;
/**
 * 分类模块
 * @author Administrator
 *
 */
public class CategoryActivity extends Activity{
	
	private RadioButton category_rb1;
	private RadioButton category_rb2;
	private RadioGroup category_rg;
	private XListView listView;
	private CustomProgressDialog customProgressDialog;
	private SearchManager searchManager;
	private CategoryInfoAdapter adapter;
	private int whichButton = 1;
	private int id = 0;
	private String url = null;
	
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			int what = msg.what;
			switch (what) {
			case AppConstants.MsgStatus.MSG_ERROR:
				if (customProgressDialog != null && customProgressDialog.isShowing())
	                customProgressDialog.dismiss();
	            ((AppException)msg.obj).makeToast(CategoryActivity.this);
				break;
			case AppConstants.MsgStatus.MSG_SUCCUSS:
				if (customProgressDialog != null && customProgressDialog.isShowing())
	                customProgressDialog.dismiss();
				//准备数据
				adapter = new CategoryInfoAdapter(CategoryActivity.this, (String[]) msg.obj);
				listView.setAdapter(adapter);
				listView.setOnItemClickListener(new ListItemListener());
				break;
			case AppConstants.MsgStatus.MSG_LOADING:
				if (customProgressDialog != null && !customProgressDialog.isShowing())
					customProgressDialog.show();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_activity_layout);
		initView();
		 
        searchManager = new SearchManager(this, mHandler);
        searchManager.searchDataFromNetwrok(AppConstants.CATEGORY);
        category_rg.setOnCheckedChangeListener(new RadioGroupListener());
	}
	private void initView(){
		category_rg = (RadioGroup) findViewById(R.id.category_rg);
		category_rb1 = (RadioButton) findViewById(R.id.category_rb1);
		category_rb2 = (RadioButton) findViewById(R.id.category_rb2);
		listView = (XListView) findViewById(R.id.custom_list);
		customProgressDialog = new CustomProgressDialog(this);
		listView.setmHeaderViewBackground(R.color.custom_list_item_bg_color);
        listView.setmFooterViewBackground(R.color.custom_list_item_bg_color);
        //不允许上拉和下拉更新
        listView.disablePullLoad();
        listView.disablePullRefreash();
       
	}
	//选中项监听
	class ListItemListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			String[] mData = adapter.getmData();
			String info = mData[arg2-1];
			id = Integer.parseInt(info);
			Intent i = new Intent();
			if(whichButton == 1){
				url = AppConstants.CATEGORY_DETAILS_EN+id;
				i.setClass(CategoryActivity.this, JobOffers.class);
			}else if(whichButton == 2){
				url = AppConstants.CATEGORY_DETAILS_TA+id;
				i.setClass(CategoryActivity.this,ResumeActivity.class);
			}
			i.putExtra("url", url);
			startActivity(i);

		}
		
	}
	
	//Radiogroup监听
	class RadioGroupListener implements OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			if(group.getCheckedRadioButtonId()==category_rb1.getId()){
				whichButton = 1;
			}else if(group.getCheckedRadioButtonId()==category_rb2.getId()){
				whichButton = 2;
			}
		}
		
	}
}
